import { createRouter, createWebHashHistory } from 'vue-router'
import {useRouter} from 'vue-router'

import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import User from '../user/User.vue'
import Student from '../user/Student.vue'
import Teacher from '../user/Teacher.vue'
import Course from '../course/Course.vue'
import CourseInformation from '../course/CourseInformation'
import CourseDate from '../course/CourseDate.vue'
import AddOil from '../study/AddOil.vue'
import Report from '../study/Report.vue'
import StudyInformation from '../study/StudyInformation.vue'
import Shouye from '../front/Shouye.vue'
import CourseDetail from '../front/CourseDetail.vue'
import Study from '../front/Study.vue'
import Mycourse from '../front/Mycourse.vue'
import Myreport from '../front/Myreport.vue'
import Myaddoil from '../front/Myaddoil.vue'

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/shouye',
    name: 'shouye',
    component: Shouye
  },
  {
    path: '/study',
    name: 'study',
    component: Study
  },
  {
    path:'/courseDetail',
    name:'CourseDetailname',
    component:CourseDetail
  },
  
  {
    path:'/login',
    component:Login
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // },
  {
    path:'/home',
    name:'home',
    component:Home,
    redirect:'/welcome',
    children:[
      {
        path:'/welcome',
        component:Welcome
      },
      {
        path:'/student',
        component:Student

      },
      {
        path:'/teacher',
        component:Teacher

      },
      {
        path:'/admin',
        component:User

      },
      {
        path:'/courselist',
        component:Course
      },
      {
        path:'/courseinformation',
        component:CourseInformation
      },
      {
        path:'/sdutyinformation',
        component:StudyInformation
      },
      {
        path:'/coursedata',
        component:CourseDate
      },
      {
        path:'/addoil',
        component:AddOil
      },
      {
        path:'/report',
        component:Report
      },
      {
        path:'/mycourse',
     
        component:Mycourse
      },
      {
        path:'/myaddoil',
     
        component:Myaddoil
      },
      {
        path:'/myreport',
        component:Myreport
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  useRouter
})
router.beforeEach((to,from,next) =>{
  if(to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})


export default router
